package com.rongdong.parkingcapture.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rongdong.parkingcapture.common.R;
import com.rongdong.parkingcapture.domain.PipelineConfig;
import com.rongdong.parkingcapture.domain.Task;
import com.rongdong.parkingcapture.domain.TransparentParameter;
import com.rongdong.parkingcapture.mapper.TaskMapper;
import com.rongdong.parkingcapture.service.ITaskService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

import static com.rongdong.parkingcapture.constant.Constants.TASK_ID;

/**
 * Task服务层
 */
@Service
public class TaskServiceImpl extends ServiceImpl<TaskMapper, Task> implements ITaskService {



    /**
     * json对象转换为字符串，进行入库
     *
     * @param task
     * @return Task
     */
    public Task objectToString(Task task) {
        //List<PipelineConfig> → String
        List<PipelineConfig> pipelineConfigList = task.getPipelineConfig();
        task.setPipelineConfigString(JSON.toJSONString(pipelineConfigList));

        //TransparentParameter → String
        TransparentParameter transparentParameter = task.getTransparentParameter();
        task.setTransparentParameterString(JSONObject.toJSONString(transparentParameter));

        return task;

    }
    //
    /**
     * 将数据库存储的字符串类型 转为json对象
     *
     * @param task
     * @return Task
     */
    public Task stringToObject(Task task) {
        //String → List<PipelineConfig>
        String pipelineConfigString = task.getPipelineConfigString();
        task.setPipelineConfig(JSON.parseArray(pipelineConfigString, PipelineConfig.class));

        //String → TransparentParameter
        String transparentParameterString = task.getTransparentParameterString();
        task.setTransparentParameter(JSON.parseObject(transparentParameterString, TransparentParameter.class));

        return task;
    }
}

